package com.example.spring.bean.load01config;

import com.example.spring.pojo.User;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 通过xml配置方式获取bean实例
 * <p> 参考 Spring beans XML 配置 bean
 *
 * <p>
 */
public class XmlSpringDemo {

    public static void main(String[] args) {

        // 告诉容器要加载的配置文件是哪个
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

        //通过 beanName 获取实例
        Object user1 = context.getBean("conf02");
        System.out.println(user1); //User{id=2, name='conf02'}

        User user2 = context.getBean(User.class);
        System.out.println(user2); //User{id=2, name='conf02'}

        //证明是同一个对象
        System.out.println(user1 == user2); //true


        /**
         * 扩展
         */
        //返回与给定类型匹配的 bean ，包括子类
        String[] beanNamesForType = context.getBeanNamesForType(User.class);
        for (String beanName : beanNamesForType) {
            System.out.println("beanName : " + beanName); //conf02
        }

        context.close();
    }
}
